import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/lget/lget.dart';
import 'package:liuxlcommon/view/loading_view.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_box.dart';
import 'package:love_newf/app/com/model/room_type.dart';
import 'package:love_newf/app/com/view/label/province_label.dart';
import 'package:love_newf/app/page/live/live_view.dart';
import 'package:love_newf/generated/assets.dart';
import 'package:love_newf/value/app_colors.dart';
import 'package:love_newf/value/app_const.dart';

import '../../../com/view/label/sex_label.dart';
import 'live_list_logic.dart';

class LiveListPage extends StatefulWidget {
  int listType;

  LiveListPage(this.listType, {super.key});

  @override
  State<LiveListPage> createState() => _LiveListPageState();
}

class _LiveListPageState extends State<LiveListPage> {
  late LiveListLogic logic;

  @override
  void initState() {
    super.initState();
    logic = Get.put(LiveListLogic(), tag: widget.listType.toString());
    logic.listType = [1, 3, 2][widget.listType];
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LiveListLogic>(
        tag: widget.listType.toString(),
        builder: (l) => LoadingView(
              datas: logic.datas,
              crossAxisCount: 2,
              childAspectRatio: 172 / 178,
              itemCall: (context, position) => itemView(position),
              isPullUp: true,
              isPullDown: true,
              onStateClick: (type) {
                if ((logic.datas?.length ?? 0) == 0) logic.datas = null;
                logic.update();
              },
              onLoad: (type) async {
                if (type == 1) {
                  logic.pageNum = 1;
                } else if (type == 2) {
                  logic.pageNum++;
                }
                return await logic.getData();
              },
              onSuccess: () {
                if (logic.datas != null) logic.update();
              },
            ));
  }

  itemView(int position) {
    var model = logic.datas![position];
    var roomType = model.roomType;
    if (roomType == RoomType.THREE || roomType == RoomType.EXCLUSIVE) {
      return itemThreeView(position);
    } else {
      return itemOtherView(position);
    }
  }

  itemThreeView(int position) {
    var model = logic.datas![position];

    return LStack([
      LImage.easy(model.anchorAvatar, double.infinity, double.infinity),
      iconRoomType(model.roomType),
      Align(
          alignment: Alignment.bottomLeft,
          child: LColumn.eazy([
            LText.easy(model.nick, 14.w, fw5, Colors.white),
            LRow.eazy([
              SexAgeLabel(model.anchorAge, model.anchorAge),
              3.lsw,
              ProvinceLabel(model.anchorProvince)
            ])
          ]).padding(left: 8.w, bottom: 12.w))
    ]).radius(14.w).onTapL(() => LivePage.openPage(model.roomId));
  }

  itemOtherView(int position) {
    var model = logic.datas![position];
    String bgImg = "";
    switch (model.roomType) {
      case RoomType.THREE:
        bgImg = "";
      case RoomType.EXCLUSIVE:
        bgImg = "";
      case RoomType.SEVEN:
        bgImg = Assets.homeHomeLiveSevenBg;
      case RoomType.ANGEL:
        bgImg = Assets.homeHomeLiveSevenAngelBg;
      case RoomType.SING:
        bgImg = Assets.homeHomeLiveSevenSongBg;
      case RoomType.TRAIN:
        bgImg = Assets.homeHomeLiveSevenBg;
      case RoomType.NINE:
        bgImg = Assets.homeHomeLiveNineAngelBg;
      case RoomType.NINE_SING:
        bgImg = Assets.homeHomeLiveSevenSongBg;
    }
    return LStack([
      iconRoomType(model.roomType),
      Center(
          child: LImage.easy(model.anchorAvatar, 80.w, 80.w, 80.w)
              .border(3.w, const Color(0x80FFFFFF)))
    ]).bgImg(bgImg).onTapL(() => LivePage.openPage(model.roomId));
  }

  Map<int, List<Color>> colors = const {
    RoomType.THREE: [Color(0xFFCBBEFF), Color(0xFFC92EFF)],
    RoomType.EXCLUSIVE: [Color(0xFFFF9772), Color(0xFFFF448B)],
    RoomType.SEVEN: [Color(0xFF81E5FF), Color(0xFF0066FF)],
    RoomType.ANGEL: [Color(0xFFFFD568), Color(0xFFFFB11A)],
    RoomType.SING: [Color(0xFFFFD18D), Color(0xFFFF720B)],
    RoomType.TRAIN: [Color(0xFFCBBEFF), Color(0xFFC92EFF)],
    RoomType.NINE: [Color(0xFF8FAEFF), Color(0xFF9022FF)],
    RoomType.NINE_SING: [Color(0xFFFF9772), Color(0xFFC92EFF)],
  };

  iconRoomType(int roomType) {
    return LText.easy(RoomType.name(roomType), 11.w, fw6, Colors.white)
        .gradient(colors[roomType]!, Alignment.centerRight)
        .paddingAll(8.w)
        .radiusOnly(topLeft: 12.w, bottomRight: 12.w);
  }

  @override
  void dispose() {
    Get.delete<LiveListLogic>(tag: widget.listType.toString());
    super.dispose();
  }
}
